package com.scraper.articles.web.transfrormer;

import com.scraper.articles.web.model.Article;
import com.scraper.articles.web.model.SourceEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ArticleTransformer {

    @Value("${articles.text.location}")
    private String textLocation;

    @Value("${articles.image.location}")
    private String imagesLocation;

    public Article transform(com.scraper.articles.domain.Article article, SourceEnum source, Long id) {
        Article resultArticle = new Article();
        resultArticle.setTitle(article.getTitle());
        resultArticle.setUrl(article.getUrl());
        resultArticle.setAuthor(article.getAuthor());
        resultArticle.setDate(article.getDate());
        resultArticle.setTextLocation(textLocation + article.getFileName());
        resultArticle.setImagesLocation(imagesLocation + article.getFileName());
        resultArticle.setSource(source);
        resultArticle.setRequestId(id);
        return resultArticle;
    }

    public List<Article> transform(List<com.scraper.articles.domain.Article> articlesList, SourceEnum source, Long id) {
        ArrayList<Article> articles = new ArrayList<>();
        for (com.scraper.articles.domain.Article article : articlesList) {
            articles.add(transform(article, source, id));
        }
        return articles;
    }
}
